package logic

import (
	"context"
	"goods/cache"
	"goods/models"

	"goods/goods"
	"goods/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CreateGoodsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCreateGoodsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateGoodsLogic {
	return &CreateGoodsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CreateGoodsLogic) CreateGoods(in *goods.CreateGoodsRequest) (*goods.CreateGoodsResponse, error) {
	// todo: add your logic here and delete this line
	var err error
	var g models.Goods
	g.GoodsTitle = in.GoodsTitle
	g.GoodsPrice = float64(in.GoodsPrice)
	g.GoodsCover = in.GoodsCover
	g.GoodsSize = in.GoodsSize
	g.GoodsStock = int(in.GoodsStock)
	g.CheckStatus = int(in.CheckStatus)
	g.Status = int(in.Status)
	if err = g.Create(); err != nil {
		logx.Error("商品创建失败")
		return nil, err
	}
	cache.SyncGoodsStock(int(g.ID), g.GoodsStock)
	return &goods.CreateGoodsResponse{
		GoodsId: int64(g.ID),
	}, nil
}
